/**
 * Created by Administrator on 2017/4/10.
 */



app.factory('globals' ['$window', '$http','$scope', function ($window, $http,$scope) {
    var globals ={};
    globals.getNav=function () {
        // 获取导航
        $http.jsonp( 'http://172.19.32.122:81/app_if/getColumns?siteId=1&parentColumnId=2&version=0&columnType=-1&jsoncallback=JSON_CALLBACK'
        ).success(function(data,header,config,status){
            console.info(2);
            $scope.navData=data;
        }).error(function(data,header,config,status){
        });
    };

    // 点击导航更新视图根据点击传入columId号

    globals.chanNav= function (coluId) {
        // debugger
        var navArr=$scope.navData.columns;
        var navIndex=0;
        for(var i=0;i<navArr.length;i++){
            if(navArr[i].columnId==coluId){
                navIndex=i;
                break;
            }
        }
        var transLf=(navIndex*2);
        $(".gation .nav ul").css("transform","translate("+-transLf+"%)");
        $(".gation .nav ul li").eq(navIndex).siblings().find("a").css("color","#fff");
        $(".gation .nav ul li").eq(navIndex).find("a").css("color","red");

        //轮播js
        $scope.myInterval = 500;
        $scope.noWrapSlides = false;
        /*  $scope.ss=false;*/
        $scope.active = 0;

        //var slides = $scope.slides = [];
        //var currIndex = 0;
        // 存储banner的相关信息
        $scope.bannerObj = [];
        // bannerImg对象
        $scope.bannerImg = [];
        // $scope.bannerTitl=[]
        //判断是否有轮播图看topCount,决定是否隐藏banner
        $http.jsonp( 'http://172.19.32.122:81/app_if/getColumns?siteId=1&parentColumnId=2&version=0&columnType=-1&jsoncallback=JSON_CALLBACK'
        ).success(function(data,header,config,status){

            $scope.columDataAll=data;
            var datlist=$scope.columDataAll.columns;
            for(var i=0;i<datlist.length;i++){
                if(datlist[i].columnId==coluId){
                    if(!datlist[i].topCount){
                        $scope.topCountShow=false;

                    }else if(datlist[i].topCount){

                        // 轮播图个数
                        // $scope.bannerCount=datlist[i].topCount;
                        bannerCount=datlist[i].topCount
                        // console.log("轮播图"+$scope.bannerCount);
                        console.log("轮播图"+bannerCount);
                        // 含有轮播图处理
                        $http.jsonp( 'http://172.19.32.122:81/app_if/getArticles?columnId='+coluId+'&lastFileId=0&count=20&rowNumber=0&version=0&jsoncallback=JSON_CALLBACK'
                        ).success(function(data1,header,config,status) {
                            // // 存储banner的相关信息
                            // $scope.bannerObj = [];
                            // $scope.bannerImg = [];
                            var counNum = 0;
                            for (var j = 0; j < data1.length; j++) {
                                if (counNum< bannerCount) { //轮播个数
                                    if (data1[j].isBigPic == 0) { //先取小图
                                        if (data1[j].picSmall) {
                                            $scope.bannerObj.push(data1[j]);
                                            $scope.bannerImg.push({img:data1[j].picSmall,title:data1[j].title,fileId:data1[j].fileId});
                                            counNum++;
                                        } else if (data1[j].picMiddle) {
                                            $scope.bannerObj.push(data1[j]);
                                            $scope.bannerImg.push({img:data1[j].picMiddle,title:data1[j].title,fileId:data1[j].fileId});
                                            counNum++;
                                        } else if (data1[j].picBig) {
                                            $scope.bannerObj.push(data1[j]);
                                            $scope.bannerImg.push({img:data1[j].picBig,title:data1[j].title,fileId:data1[j].fileId});
                                            counNum++;
                                        }
                                    } else if (data1[j].isBigPic == 1) {//先中图
                                        if (data1[j].picMiddle) {
                                            $scope.bannerObj.push(data1[j]);
                                            $scope.bannerImg.push({img:data1[j].picMiddle,title:data1[j].title,fileId:data1[j].fileId});
                                            counNum++;
                                        } else if (data1[j].picBig) {
                                            $scope.bannerObj.push(data1[j]);
                                            $scope.bannerImg.push({img:data1[j].picBig,title:data1[j].title,fileId:data1[j].fileId});
                                            counNum++;
                                        } else if (data1[j].picSmall) {
                                            $scope.bannerObj.push(data1[j]);
                                            $scope.bannerImg.push({img:data1[j].picSmall,title:data1[j].title,fileId:data1[j].fileId});
                                            counNum++;
                                        }
                                    }
                                }
                            }
                            $scope.topCountShow = true;
                            if (!$scope.bannerImg[0]) {
                                $scope.topCountShow = false;
                            }


                        }).error(function(data1,header,config,status){
                        });
                    }
                }
            }

            // //实现跳转，获取数据,需要先去除轮播图含有的对象
            $http.jsonp('http://172.19.32.122:81/app_if/getArticles?columnId=' + coluId + '&lastFileId=0&count=20&rowNumber=0&version=0&jsoncallback=JSON_CALLBACK'
            ).success(function (data, header, config, status) {

                var datas=data;
                if(bannerCount){
                    for(var i=0;i<$scope.bannerObj.length;i++){
                        for(var j=0;j<datas.length;j++){
                            if(datas[j].fileId==$scope.bannerObj[i].fileId){
                                datas.splice(j,1);
                                console.log(datas[j].fileId);
                            }
                        }
                    }
                }
                $scope.Hdata = datas; //所有数据中去除banner中对象
            }).error(function (data, header, config, status) {
            });



        }).error(function(data,header,config,status){
        });

    };

    return globals;
}]);